package com.wangjie.controller;

import com.wangjie.page.TableDataInfo;
import com.wangjie.pojo.SysFile;
import com.wangjie.response.AjaxResult;
import com.wangjie.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件信息管理
 *
 * @author jieshao
 * @date 2021/11/17 16:04
 */
@Controller
public class SysFileController extends BaseController {
    @Autowired
    private SysFileService fileService;

    @GetMapping("/index.html")
    public String index() {
        return "index";
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap map) {
        map.put("file", fileService.selectById(id));
        return "detail";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFile file) {
        startPage();
        List<SysFile> list = fileService.selectFileList(file);
        return getDataTable(list);
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(fileService.deleteByIds(ids));
    }

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws Exception {
        return toAjax(fileService.upload(file));
    }

    @GetMapping("/download")
    public void download(Integer id, HttpServletResponse response) {
        fileService.download(id, response);
    }

    @GetMapping("/view")
    public void view(Integer id, HttpServletResponse response) {
        fileService.view(id, response);
    }
}